<?php
	/**
	 * 商品评论控制器
	 */
	 class CommentController extends Controller{
	 	
		private $model;
		
		public function __init(){
			$this->model = K('GoodsComment');
		}
		
		/**
		 * 查询当前用户是否已经购物此商品和是否已经评论过
		 */
		 public function isBuyComm(){
		 	if(!IS_AJAX) $this->error('非法请求');
		 	$uid = session('uid');
			$gid = Q('get.gid',0,'intval');
			//查询当前用户是否购物过此商品
			$result = M()->join('__goods_order__ go JOIN __order_list__ ol ON ol.order_oid=go.oid')->where("go.users_uid={$uid} AND ol.goods_gid={$gid} AND order_status='交易完成'")->find();
			//判断是否已经评论过
			$commented = K('GoodsComment')->where("goods_gid={$gid} AND users_uid={$uid}")->find();
			if(!$result){
				$this->ajax(array('message'=>'对不起您还没有购物此商品,只有购物此商品的用户才能评论','status'=>0));
			}
			//如果已经购买了此商品但是没有进行评论那么可以进行评论
			if($result && $commented){
				$this->ajax(array('message'=>'您已经对此商品进行过评论','status'=>0));
			}
			$this->ajax(array('status'=>1));
		 }
		 
		 /**
		  * 提交一个评论
		  */
		  public function addComment(){
		  	$data = $this->model->addComment();
		  	if($data){
		  		$data['gc_time'] = date('Y-m-d H:i:s',$data['gc_time']);
				$data['combinestr'] = explode(',',$data['combinestr']);
				$data['username'] = substr_replace($data['username'],'***',0,3);
				$data['username'] = substr_replace($data['username'],'**',-2);				
		  		$this->ajax(array('message'=>'发表评论成功','status'=>1,'data'=>$data));
		  	}else{
		  		$this->ajax(array('message'=>$this->model->error,'status'=>0));
		  	}
		  }
		 
		 
		
		
	 }
	